package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Session.GetSuggestionsRes;
import net.ib.mntalk.protocol.Session.GetUserAccessControlReq;
import net.ib.mntalk.protocol.Session.GetUserAccessControlRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.SuggestionDatabaseHelper;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class GetUserAccessControlTask extends AsyncNetTask{
	
	private GetUserAccessControlReq mGetUserAccessControlReq;
	private int index, type;

	public GetUserAccessControlTask(Context _context, Handler _uiHandler, GetUserAccessControlReq getUserAccessControlReq) {
		super(_context, _uiHandler);
		mContext = _context;
		mUiHandler = _uiHandler;
		this.mGetUserAccessControlReq = getUserAccessControlReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
		.setType(TcpEnvelope.REQUEST)
		.setMessage(mGetUserAccessControlReq)
		.setMethodId(Session.User.GetUserAccessControl)
		.build();
	return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject,	Object receivedObject) {
		MTalkLogWriter.d("GetUserAccessControlTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetUserAccessControlRes getUserAccessControlRes = (GetUserAccessControlRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetUserAccessControlTask ResponseCode : " + getUserAccessControlRes.getResponseCode());
		
		if(getUserAccessControlRes.getResponseCode() == Common.SUCCESS){
			// TODO : 디비작업
			Message msg = mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_USER_ACCESSCONTROL_SUCCESS);
			msg.obj = getUserAccessControlRes.getAccessControl();
			mUiHandler.sendMessageAtFrontOfQueue(msg);
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_USER_ACCESSCONTROL_FAIL));
		}
	}

}
